/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backoffice;

import ejb.CategoryBean;
import ejb.CategoryBeanLocal;
import entities.Category;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import web.Constants;

/**
 *
 * @author Alejandro
 */
@ManagedBean
@SessionScoped
public class BackofficeCategoryManagedBean {
        private Category categoryForInsert;
    private Category categoryForDelete;
    @EJB
    private CategoryBeanLocal categoryBean;

    public BackofficeCategoryManagedBean() {
        categoryBean = new CategoryBean();
    }
    
    
    
    public Category getCategoryForInsert() {
        if(categoryForInsert==null){
            categoryForInsert = new Category();
        }
        return categoryForInsert;
    }

    public void setCategoryForInsert(Category bcCategorySelected) {
        this.categoryForInsert = bcCategorySelected;
    }
    
    public Category getCategoryForDelete() {
        return categoryForDelete;
    }
    
    public void setCategoryForDelete(Category categoryForDelete) {
        this.categoryForDelete = categoryForDelete;
    }
   
    public List<Category> childrenFor(Category parent){
        return categoryBean.findByIdParentCategory(parent);
    }
    
    public void unsubscribeCategory(Category category){
        
      List<Category> children=  childrenFor(category);
      if(children!=null && !children.isEmpty()){
          for(Category child:children){
              categoryBean.markAsDelete(child);
          }
      }
      categoryBean.markAsDelete(category);     
       FacesContext.getCurrentInstance().addMessage("mssgs", new FacesMessage(FacesMessage.SEVERITY_INFO,"Información", "La categoría "+category.getName()+" se ha ELIMINADO correctamente"));
             
        //HECHO FIXME TODO poner el MenuBean.panelMenu a null para que se regenere
       Constants.setNeedUpdateMenu(Boolean.TRUE);
    }
    
    
     public void createCategory(Category category){
         if(category.getName()==null || category.getName().isEmpty()){
              FacesContext.getCurrentInstance().addMessage("mssgs", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "El nombre no puede ser vacío"));        
         }else{
              categoryBean.insert(category);
              FacesContext.getCurrentInstance().addMessage("mssgs", new FacesMessage(FacesMessage.SEVERITY_INFO,"Información", "La categoría "+category.getName()+" se ha CREADO correctamente"));
              category.setName("");
              // HECHO FIXME TODO poner el MenuBean.panelMenu a null para que se regenere
               Constants.setNeedUpdateMenu(Boolean.TRUE);
         }
        
        
     }
     public List<Category> childrenCategories(){
        return categoryBean.findAllChildren();
     }
     
      public SelectItem[] categoryList() {
          List<Category> cat = categoryBean.findAllChildren();
          SelectItem[] items = new SelectItem[cat.size()+1];
          items[0] =new SelectItem("", "Todas");
       
          for(int i=0;i!=cat.size();i++){
              items[i+1]= new SelectItem(cat.get(i).getName());
          }
          
        return items;
    }
}
